#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QMessageBox>
#include "CxTransFacade.h"
#include <QFileDialog>
#include <string.h>


MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    connect(this,SIGNAL(sig_currentFrame(int,double)),this,SLOT(slot_cuurentFrame(int,double)));
    connect(ui->horizontalSlider_1,SIGNAL(valueChanged(int)),this,SLOT(slot_setframe_1(int)));
    connect(ui->horizontalSlider_2,SIGNAL(valueChanged(int)),this,SLOT(slot_setframe_2(int)));
    connect(ui->horizontalSlider_3,SIGNAL(valueChanged(int)),this,SLOT(slot_setframe_3(int)));
    connect(ui->horizontalSlider_4,SIGNAL(valueChanged(int)),this,SLOT(slot_setframe_4(int)));
}

MainWindow::~MainWindow()
{
    delete ui;
}

void                            MainWindow::slot_setframe_1(int number)
{
    if(!m_facade->isBuilded())
    {
        PushStr("Please set up the server first...");
        ui->horizontalSlider_1->setValue(0);
        return ;
    }
    double value=(double)number / 1000.0;

    m_facade->SLOT_setframecount(value,0);
}
void                            MainWindow::slot_setframe_2(int number)
{
    if(!m_facade->isBuilded())
    {
        PushStr("Please set up the server first...");
        ui->horizontalSlider_2->setValue(0);
        return ;
    }

    double value=(double)number / 1000.0;

    m_facade->SLOT_setframecount(value,1);

}
void                            MainWindow::slot_setframe_3(int number)
{
    if(!m_facade->isBuilded())
    {
        PushStr("Please set up the server first...");
        ui->horizontalSlider_3->setValue(0);
        return ;
    }

    double value=(double)number / 1000.0;

    m_facade->SLOT_setframecount(value,2);
}
void                           MainWindow:: slot_setframe_4(int number )
{
    if(!m_facade->isBuilded())
    {
        PushStr("Please set up the server first...");
        ui->horizontalSlider_4->setValue(0);
        return ;
    }

    double value=(double)number / 1000.0;

    m_facade->SLOT_setframecount(value,3);
}




void  MainWindow::slot_cuurentFrame(int number, double value)
{
    int  current=1000.0*value;

    switch(number)
    {
    case 0:
        ui->progressBar_1->setValue(current);
        break;
    case 1:
        ui->progressBar_2->setValue(current);
        break;
    case 2:
        ui->progressBar_3->setValue(current);
        break;
    case 3:
        ui->progressBar_4->setValue(current);
        break;
    default:
        break;
    }
}


void MainWindow::SetFacade(ImitateTansfer::CxTransFacade *facade)
{
    m_facade=facade;
    m_facade->sig_connectclient.connect(boost::bind(&MainWindow::slot_clientconnect,this,_1));
    m_facade->sig_portclose.connect(boost::bind(&MainWindow::slot_clientdisconnect,this,_1));
    m_facade->sig_currentframe.connect(boost::bind(&MainWindow::sig_currentFrame,this,_1,_2));
}

 void                MainWindow::PushStr(const char* str)
 {
     QString _str(tr(str));

     ui->listWidget->addItem(_str);

 }


void MainWindow::on_commandLinkButton_clicked()
{
    if(!m_facade->isBuilded())
    {
        PushStr("Please set up the server first...");
        return ;
    }


    QString filename;
    filename=QFileDialog::getOpenFileName(this,
                                          tr("打开视频"),"",
                                          tr("video (*.avi)")
                                         );

    if(m_facade->SLOT_openfile(filename.toStdString().c_str(),0))
        PushStr("通道一已经打开视频....");
    else PushStr("通道一打开视频失败......");

}

void MainWindow::on_commandLinkButton_2_clicked()
{
    if(!m_facade->isBuilded())
    {
        PushStr("Please set up the server first...");
        return ;
    }


    QString filename;
    filename=QFileDialog::getOpenFileName(this,
                                          tr("打开视频"),"",
                                          tr("video (*.avi)")
                                         );
    if(m_facade->SLOT_openfile(filename.toStdString().c_str(),1))
        PushStr("通道二已经打开视频....");
    else PushStr("通道二打开视频失败.....");
}

void MainWindow::on_commandLinkButton_3_clicked()
{
    if(!m_facade->isBuilded())
    {
        PushStr("Please set up the server first...");
        return ;
    }


    QString filename;
    filename=QFileDialog::getOpenFileName(this,
                                          tr("打开视频"),"",
                                          tr("video (*.avi)")
                                         );
    if(m_facade->SLOT_openfile(filename.toStdString().c_str(),2))
        PushStr("通道三已经打开视频....");
    else PushStr("通道三打开视频失败....");
}

void MainWindow::on_commandLinkButton_4_clicked()
{
    if(!m_facade->isBuilded())
    {
        PushStr("Please set up the server first...");
        return ;
    }

    QString filename;
    filename=QFileDialog::getOpenFileName(this,
                                          tr("打开视频"),"",
                                          tr("video (*.avi)")
                                         );

    if(m_facade->SLOT_openfile(filename.toStdString().c_str(),3))
        PushStr("通道四已经打开视频....");
    else PushStr("通道四打开视频失败......");
}
void MainWindow::slot_clientconnect(size_t number)
{
    char buffer[128];
    sprintf(buffer,"Client %d have been connected...",number+1);
    PushStr(buffer);
}

void MainWindow::slot_clientdisconnect(size_t number)
{

    char buffer[128];
    sprintf(buffer,"Client %d have been break...",number+1);

    PushStr(buffer);
}

void MainWindow::on_pushButton_clicked()
{
    if(!m_facade->isBuilded())
    {
        m_facade->BuildPart(4);
        PushStr("Server have been set up......");
    }
}

void MainWindow::on_pushButton_2_clicked()
{
    if(m_facade->isBuilded())
    {
        m_facade->DestroyPart();
        PushStr("Server have been shutdown......");
    }
}

void MainWindow::on_pushButton_3_clicked()
{
    ui->listWidget->clear();
}
